<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>瑞吉外卖管理端</title>
  <link rel="shortcut icon" href="../../favicon.ico">
  <!-- 引入样式 -->
  <link rel="stylesheet" href="../../plugins/element-ui/index.css" />
  <link rel="stylesheet" href="../../styles/common.css">
  <link rel="stylesheet" href="../../styles/login.css">
  <link rel="stylesheet" href="../../styles/icon/iconfont.css" />
</head> 

<body>
  <div class="login" id="login-app">
    <div class="login-box">
      <img src="../../images/login/login-l.png" alt="">
      <div class="login-form">

        <!--登录表单-->
        <!--
          ref="loginForm"：表单名称，校验的时候使用
          :model="loginForm"：绑定表单数据对象
          :rules="loginRules"：绑定表单验证规则
        -->
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" >
          <div class="login-form-title">
            <img src="../../images/login/logo.png" style="width:139px;height:42px;" alt="" />
          </div>

          <!--表单项: 登录账号-->
          <!--
            prop="username": 对应loginRules中的属性，若想要开启校验，则必须设置此项目
            auto-complete="off": 禁止input文本输入框记录下之前提交表单的信息
            prefix-icon: 输入框头部图标
            v-model="loginForm.username": 双向绑定表单数据中username值
          -->
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号" maxlength="20"
              prefix-icon="iconfont icon-user" />
          </el-form-item>

          <!--表单项: 登录密码-->
          <!--
            prop="password": 对应loginRules中的属性，若想要开启校验，则必须设置此项目
            v-model="loginForm.password": 双向绑定表单数据中password值
            @keyup.enter.native="handleLogin": 回车触发handleLogin方法
          -->
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" type="password" placeholder="密码" prefix-icon="iconfont icon-lock" maxlength="20"
              @keyup.enter.native="handleLogin" />
          </el-form-item>

          <!--按钮: 登录-->
          <!--
            :loading: 控制loading效果
            @click.native.prevent: 点击按钮，触发handleLogin方法
          -->
          <el-form-item style="width:100%;">
            <el-button :loading="loading" class="login-btn" size="medium" type="primary" style="width:100%;"
              @click.native.prevent="handleLogin">
              <span v-if="!loading">登录</span>
              <span v-else>登录中...</span>
            </el-button>
          </el-form-item>

        </el-form>
      </div>
    </div>

  </div>

  <!-- 开发环境版本，包含了有帮助的命令行警告 -->
  <script src="../../plugins/vue/vue.js"></script>
  <!-- 引入组件库 -->
  <script src="../../plugins/element-ui/index.js"></script>
  <!-- 引入axios -->
  <script src="../../plugins/axios/axios.min.js"></script>
  <script src="../../js/request.js"></script>
<!--  <script src="../../js/validate.js"></script>-->
  <script src="../../api/login.js"></script>

  <script>
    new Vue({
      el: '#login-app',
      data() {
        return {
          // data: 登录表单数据对象
          loginForm:{
            username: 'admin',
            password: '123456'
          },

          // data: loading效果变量
          loading: false
        }
      },
      computed: {
        // data: 登录表单校验规则
        loginRules() {
          const validateUsername = (rule, value, callback) => {
            if (value.length < 1 ) {
              callback(new Error('请输入用户名'))
            } else {
              callback()
            }
          }
          const validatePassword = (rule, value, callback) => {
            if (value.length < 6) {
              callback(new Error('密码必须在6位以上'))
            } else {
              callback()
            }
          }
          return {
            'username': [{ 'validator': validateUsername, 'trigger': 'blur' }],
            'password': [{ 'validator': validatePassword, 'trigger': 'blur' }]
          }
        }
      },
      created() {
      },
      methods: {
        // method: 当点击登录按钮或回车时触发
        async handleLogin() {
          // 判断loginForm中所有控件是否都通过了规则验证
          this.$refs.loginForm.validate(async (valid) => {
            if (valid) {
              this.loading = true

              // 调用用户登录接口
              let res = await loginApi(this.loginForm)
              if (String(res.code) === '1') {//1表示登录成功
                // 将用户的信息存入localStorage中
                localStorage.setItem('userInfo',JSON.stringify(res.data))

                // 跳转到外卖管理端首页
                window.location.href= '/backend/index.html'
              } else {
                this.$message.error(res.msg)
                this.loading = false
              }
            }
          })
        }
      }
    })
  </script>
</body>

</html>
